//
//  HJInformationPresent.m
//  KaiChat
//
//  Created by lx on 2024/3/13.
//

#import "HJInformationPresent.h"
#import "HJNewsTabModel.h"
#import "HJUserNewsTagModel.h"
#import "HJIM.h"

@implementation HJInformationPresent

+ (void)followUser:(NSString *)userId complete:(void (^__nullable)(NSError * _Nullable error))complete {
    [HJNetworking.shareNetwork POSTBodyJSON:API_newsFollow parameters:@{
        @"user_id": userId
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}


+ (void)loadNewsListWithType:(NSInteger)type newsTypeId:(NSInteger)newsTypeId tags:(nullable NSString *)tags pageIndex:(NSInteger)pageIndex complete:(void (^__nullable)(NSInteger serverPageSize, NSArray <HJInformationModel *>* _Nullable news,  NSArray <HJFollowModel *>* _Nullable recommendUsers, NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:@(type) forKey:@"type"];
    if (tags.intValue > 0) {
        [parameters setValue:tags forKey:@"tag_id"];
    }
    [parameters setValue:@(pageIndex) forKey:@"page"];
    [parameters setValue:@(newsTypeId) forKey:@"news_type_id"];
    
    [HJNetworking.shareNetwork POSTBodyJSON:API_newsList parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        
        NSArray *contents_list = data[@"contents_list"];
        NSMutableArray *news;
        
        if ([contents_list isKindOfClass:NSArray.class] && contents_list.count) {
            news = [NSMutableArray array];
            for (NSDictionary *dict in contents_list) {
                [news addObject:[HJInformationModel modelWithDict:dict]];
            }
        }
        
        NSArray *recommend_users = data[@"recommend_users"];
        NSMutableArray *recommendUsers;
        
        if ([recommend_users isKindOfClass:NSArray.class] && recommend_users.count) {
            recommendUsers = [NSMutableArray array];
            for (NSDictionary *dict in recommend_users) {
                [recommendUsers addObject:[HJFollowModel modelWithDict:dict]];
            }
        }
        !complete ?: complete([data[@"size"] integerValue], news, recommendUsers, nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(0, nil, nil, error);
    }];
}

+ (void)postNewsWithTitle:(nullable NSString *)title content:(NSString *)content images:(nullable NSArray *)images videos:(nullable NSArray *)videos complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    if (title) {
        [parameters setValue:@(4) forKey:@"data_type"];
    } else {
        [parameters setValue:@(3) forKey:@"data_type"];
    }
    
    [parameters setValue:title forKey:@"title"];
    [parameters setValue:content forKey:@"content"];
    [parameters setValue:images forKey:@"images"];
    [parameters setValue:videos forKey:@"videos"];
    
    [HJNetworking.shareNetwork POSTBodyJSON:API_postNews parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
    
}

+ (void)loadNewsTagsListWithTypeNewsTypeId:(NSInteger)newsTypeId complete:(void (^__nullable)(NSArray * _Nullable datas))complete {
    [HJNetworking.shareNetwork POSTBodyJSON:API_newsTagsList parameters:@{
        @"type_id": @(newsTypeId)
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        
        dispatch_async_sql_queue(^{
            
            NSMutableArray *all = [NSMutableArray array];
            NSMutableArray *user = [NSMutableArray array];
            
            NSMutableDictionary *userTagDict = [NSMutableDictionary dictionary];
            
            HJUserNewsTagModel *userTagModel = [HJUserNewsTagModel bg_find:[HJUserNewsTagModel bg_tableName] where:[NSString stringWithFormat:@"where %@=%@", bg_sqlKey(@"key"), bg_sqlValue([HJUserNewsTagModel getKeyWithUserId:HJUserInfo.userId typeId:toString(@(newsTypeId))])]].firstObject;
            for (NSDictionary *dict in data[@"tags_user"]) {
                HJNewsTabModel *model = [HJNewsTabModel modelWithDict:dict];
                model.isLike = YES;
                model.isSelect = [model.tagName isEqualToString:userTagModel.selectTag];
                if (!isEmptyString(model.tagName) && model.tagID) {
                    [user addObject:model];
                    [userTagDict setObject:model forKey:toString(dict[@"tag_id"])];
                }
            }
            
            for (NSDictionary *dict in data[@"tags_all"]) {
                if (![userTagDict objectForKey:toString(dict[@"tag_id"])]) {
                    HJNewsTabModel *model = [HJNewsTabModel modelWithDict:dict];
                    if (!isEmptyString(model.tagName) && model.tagID) {
                        [all addObject:model];
                    }
                }
            }

            userTagModel.userId = HJUserInfo.userId;
            userTagModel.userTags = user;
            userTagModel.allTags = all;
            [userTagModel bg_saveOrUpdate];
            
            dispatch_async_main_queue(^{
                !complete ?: complete(@[user, all]);
            });
        });
        
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil);
    }];
}

+ (void)loadRecommendUsersDataComplete:(void (^__nullable)(NSArray <HJFollowModel *>* _Nullable recommendList))complete {
    [HJNetworking.shareNetwork POSTBodyJSON:API_newsRecommendUsers parameters:@{
        @"page": @1,
        @"size": @5
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        
        NSArray *recommend_users = data[@"recommend_users"];
        NSMutableArray *recommendList = nil;
        if (recommend_users.count) {
            recommendList = [NSMutableArray array];
            for (NSDictionary *dict in recommend_users) {
                [recommendList addObject:[HJFollowModel modelWithDict:dict]];
            }
        }
        !complete ?: complete(recommendList);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil);
    }];
}

+ (void)updateUserTagsWithTypeNewsTypeId:(NSInteger)newsTypeId tagId:(NSInteger)tagId isAdd:(BOOL)isAdd complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:@(newsTypeId) forKey:@"type_id"];
    if (isAdd) {
        [parameters setObject:@(tagId) forKey:@"tag_id"];
    } else {
        [parameters setObject:@(tagId) forKey:@"del_tag_id"];
    }
    [HJNetworking.shareNetwork POSTBodyJSON:API_updateUserTags parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)loadModuleListDataComplete:(void (^__nullable)(NSArray * _Nullable datas))complete {
    [HJNetworking.shareNetwork POSTBodyJSON:API_moduleList parameters:nil success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        NSArray *module_list = data[@"module_list"];
        if (module_list) {
            [USERDEFAULTS setObject:module_list forKey:moduleListKey];
        }
        !complete ?: complete(module_list);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil);
    }];
}


+ (void)newsLikeWithType:(NSInteger)type newsId:(NSString *)newsId isLike:(BOOL)isLike complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:@(type) forKey:@"target_type"];
    [parameters setValue:@(!isLike) forKey:@"op"];
    [parameters setValue:newsId forKey:@"target_id"];

    [HJNetworking.shareNetwork POSTBodyJSON:API_newsLike parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)loadUserHomePageDataWithUserId:(NSString *)userId complete:(void (^__nullable)(HJChatUserModel * _Nullable userModel))complete {
 
    [HJNetworking.shareNetwork GET:API_homePage parameters:@{
        @"user_id": userId
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        dispatch_async_sql_queue(^{
            HJUserModel *userModel = [HJUserModel currentAccountModel];
            userModel.posts = [data[@"posts"] integerValue];
            userModel.fans = [data[@"fans"] integerValue];
            userModel.follows = [data[@"follows"] integerValue];
            userModel.friends = [data[@"friends"] integerValue];
            [userModel bg_saveOrUpdate];
            
            HJChatUserModel *chatUserModel = [[HJChatUserModel alloc] init];
            chatUserModel.posts = userModel.posts;
            chatUserModel.userId = userId;
            chatUserModel.fans = userModel.fans;
            chatUserModel.follows = userModel.follows;
            chatUserModel.friends = userModel.friends;
            chatUserModel.avatar = data[@"profile_picture"];
            chatUserModel.brief = data[@"brief"];
            chatUserModel.nickname = data[@"nickname"];
            chatUserModel.walletAddress = data[@"wallet_address"];
            chatUserModel.isFollow = [data[@"is_follow"] boolValue];
            chatUserModel.relationship = [data[@"relationship"] intValue];
            chatUserModel.sessionId = toString(data[@"session_id"]);
            [chatUserModel bg_saveOrUpdate];
            
            dispatch_async_main_queue(^{
                !complete ?: complete(chatUserModel);
            });
        });
   
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil);
    }];
}

+ (void)statisticsUserShareDataWithType:(NSInteger)type newsId:(NSString *)newsId complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:@(type) forKey:@"data_type"];
    [parameters setValue:newsId forKey:@"id"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_newsShareStatistics parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}


+ (void)deletePostDataWithNewsId:(NSString *)newsId complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:newsId forKey:@"post_id"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_deletePost parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)hidePostDataWithNewsId:(NSString *)newsId complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:newsId forKey:@"post_id"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_hidePost parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}


+ (void)statisticsNew_7_24ViewDataWithType:(NSInteger)type newsIds:(NSArray *)newsIds complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:@(type) forKey:@"data_type"];
    [parameters setValue:newsIds forKey:@"ids"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_7_24newsViewStatistics parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}


+ (void)loadNewswDataWithType:(NSInteger)type newsId:(NSString *)newsId complete:(void (^__nullable)(NSDictionary * _Nullable data))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:@(type) forKey:@"data_type"];
    [parameters setValue:newsId forKey:@"id"];
    [HJNetworking.shareNetwork GET:API_informationDetail parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(data);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil);
    }];
}


+ (void)loadHomeNewsListWithUserId:(NSString *)userId type:(NSInteger)type  pageIndex:(NSInteger)pageIndex complete:(void (^__nullable)(NSInteger totalCount, NSArray <HJInformationModel *>* _Nullable news, NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:@(type) forKey:@"type"];
    [parameters setValue:@(pageIndex) forKey:@"page"];
    [parameters setValue:@(20) forKey:@"size"];
    [parameters setValue:userId forKey:@"user_id"];

    [HJNetworking.shareNetwork POSTBodyJSON:API_homeNewsList parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        
        NSArray *contents_list = data[@"contents_list"];
        NSMutableArray *news;
        
        if ([contents_list isKindOfClass:NSArray.class] && contents_list.count) {
            news = [NSMutableArray array];
            for (NSDictionary *dict in contents_list) {
                [news addObject:[HJInformationModel modelWithDict:dict]];
            }
        }
        !complete ?: complete([data[@"total_count"] integerValue], news, nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(0, nil, error);
    }];
}

+ (void)loadNewsStatisticsDataWithType:(NSInteger)type newsId:(NSString *)newsId complete:(void (^__nullable)(NSDictionary * _Nullable data))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:@(type) forKey:@"data_type"];
    [parameters setValue:newsId forKey:@"id"];
    [HJNetworking.shareNetwork GET:API_homeNewsStatistics parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(data);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil);
    }];
}



@end
